package com.vates.springapp.repository.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.vates.springapp.domain.Cliente;
import com.vates.springapp.repository.BaseDao;


@Repository(value="clienteDAO")
public class ClienteDaoImpl implements BaseDao<Cliente> {
	
	
	
private EntityManager em;

	
	/*
	 * Sets the entity manager
	 */
	
	@PersistenceContext
	public void setEntityManager(EntityManager em)
	{
		this.em= em;
		
	}
	
	@Override
	@Transactional(readOnly=true)
	public Cliente nuevo(Cliente entidad) {
		
		return em.merge(entidad);
	}

	@Override
	public Cliente buscarPorId(Long id) {
		
		return em.find(Cliente.class,id);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public List<Cliente> buscarTodos() {

		return em.createQuery("select c from cliente c order by c.id").getResultList();
		
		
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public List<Cliente> buscarPorNombre(String nombre) {
		
		return em.createQuery("select c from Cliente c where c.nombre like :nombre order by c.id").getResultList();
		
		
	}

	@Override
	public void borrar(Cliente entidad) {
		
	//	em.remove(entidad);

	}
 
	
	

}
